Python 시각화 시작하기

Python 시각화 라이브러리

  • matplotlib
  • seaborn
  • bokeh

Prerequisite

inline graphics (Jupyter Notebook)

%matplotlib inline

Backend format


In [62]:
%config InlineBackend.figure_formats


Out[62]:
{'svg'}

In [63]:
%config InlineBackend.figure_formats = ['svg']

inline figure size


In [64]:
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)

Basic plot

plot(y)

  • x = range(len(y))

In [65]:
plt.plot([1,2,3,4])
plt.show()


plot(x, y)


In [66]:
plt.plot([1,2,3,4], [1,4,9,16])
plt.show()


plot(xy, color)


In [67]:
plt.plot([1,2,3,4], [1,4,9,16], 'r')
plt.show()



In [68]:
mpl.colors.cnames


Out[68]:
{u'aliceblue': u'#F0F8FF',
 u'antiquewhite': u'#FAEBD7',
 u'aqua': u'#00FFFF',
 u'aquamarine': u'#7FFFD4',
 u'azure': u'#F0FFFF',
 u'beige': u'#F5F5DC',
 u'bisque': u'#FFE4C4',
 u'black': u'#000000',
 u'blanchedalmond': u'#FFEBCD',
 u'blue': u'#0000FF',
 u'blueviolet': u'#8A2BE2',
 u'brown': u'#A52A2A',
 u'burlywood': u'#DEB887',
 u'cadetblue': u'#5F9EA0',
 u'chartreuse': u'#7FFF00',
 u'chocolate': u'#D2691E',
 u'coral': u'#FF7F50',
 u'cornflowerblue': u'#6495ED',
 u'cornsilk': u'#FFF8DC',
 u'crimson': u'#DC143C',
 u'cyan': u'#00FFFF',
 u'darkblue': u'#00008B',
 u'darkcyan': u'#008B8B',
 u'darkgoldenrod': u'#B8860B',
 u'darkgray': u'#A9A9A9',
 u'darkgreen': u'#006400',
 u'darkgrey': u'#A9A9A9',
 u'darkkhaki': u'#BDB76B',
 u'darkmagenta': u'#8B008B',
 u'darkolivegreen': u'#556B2F',
 u'darkorange': u'#FF8C00',
 u'darkorchid': u'#9932CC',
 u'darkred': u'#8B0000',
 u'darksage': u'#598556',
 u'darksalmon': u'#E9967A',
 u'darkseagreen': u'#8FBC8F',
 u'darkslateblue': u'#483D8B',
 u'darkslategray': u'#2F4F4F',
 u'darkslategrey': u'#2F4F4F',
 u'darkturquoise': u'#00CED1',
 u'darkviolet': u'#9400D3',
 u'deeppink': u'#FF1493',
 u'deepskyblue': u'#00BFFF',
 u'dimgray': u'#696969',
 u'dimgrey': u'#696969',
 u'dodgerblue': u'#1E90FF',
 u'firebrick': u'#B22222',
 u'floralwhite': u'#FFFAF0',
 u'forestgreen': u'#228B22',
 u'fuchsia': u'#FF00FF',
 u'gainsboro': u'#DCDCDC',
 u'ghostwhite': u'#F8F8FF',
 u'gold': u'#FFD700',
 u'goldenrod': u'#DAA520',
 u'gray': u'#808080',
 u'green': u'#008000',
 u'greenyellow': u'#ADFF2F',
 u'grey': u'#808080',
 u'honeydew': u'#F0FFF0',
 u'hotpink': u'#FF69B4',
 u'indianred': u'#CD5C5C',
 u'indigo': u'#4B0082',
 u'ivory': u'#FFFFF0',
 u'khaki': u'#F0E68C',
 u'lavender': u'#E6E6FA',
 u'lavenderblush': u'#FFF0F5',
 u'lawngreen': u'#7CFC00',
 u'lemonchiffon': u'#FFFACD',
 u'lightblue': u'#ADD8E6',
 u'lightcoral': u'#F08080',
 u'lightcyan': u'#E0FFFF',
 u'lightgoldenrodyellow': u'#FAFAD2',
 u'lightgray': u'#D3D3D3',
 u'lightgreen': u'#90EE90',
 u'lightgrey': u'#D3D3D3',
 u'lightpink': u'#FFB6C1',
 u'lightsage': u'#BCECAC',
 u'lightsalmon': u'#FFA07A',
 u'lightseagreen': u'#20B2AA',
 u'lightskyblue': u'#87CEFA',
 u'lightslategray': u'#778899',
 u'lightslategrey': u'#778899',
 u'lightsteelblue': u'#B0C4DE',
 u'lightyellow': u'#FFFFE0',
 u'lime': u'#00FF00',
 u'limegreen': u'#32CD32',
 u'linen': u'#FAF0E6',
 u'magenta': u'#FF00FF',
 u'maroon': u'#800000',
 u'mediumaquamarine': u'#66CDAA',
 u'mediumblue': u'#0000CD',
 u'mediumorchid': u'#BA55D3',
 u'mediumpurple': u'#9370DB',
 u'mediumseagreen': u'#3CB371',
 u'mediumslateblue': u'#7B68EE',
 u'mediumspringgreen': u'#00FA9A',
 u'mediumturquoise': u'#48D1CC',
 u'mediumvioletred': u'#C71585',
 u'midnightblue': u'#191970',
 u'mintcream': u'#F5FFFA',
 u'mistyrose': u'#FFE4E1',
 u'moccasin': u'#FFE4B5',
 u'navajowhite': u'#FFDEAD',
 u'navy': u'#000080',
 u'oldlace': u'#FDF5E6',
 u'olive': u'#808000',
 u'olivedrab': u'#6B8E23',
 u'orange': u'#FFA500',
 u'orangered': u'#FF4500',
 u'orchid': u'#DA70D6',
 u'palegoldenrod': u'#EEE8AA',
 u'palegreen': u'#98FB98',
 u'paleturquoise': u'#AFEEEE',
 u'palevioletred': u'#DB7093',
 u'papayawhip': u'#FFEFD5',
 u'peachpuff': u'#FFDAB9',
 u'peru': u'#CD853F',
 u'pink': u'#FFC0CB',
 u'plum': u'#DDA0DD',
 u'powderblue': u'#B0E0E6',
 u'purple': u'#800080',
 u'red': u'#FF0000',
 u'rosybrown': u'#BC8F8F',
 u'royalblue': u'#4169E1',
 u'saddlebrown': u'#8B4513',
 u'sage': u'#87AE73',
 u'salmon': u'#FA8072',
 u'sandybrown': u'#FAA460',
 u'seagreen': u'#2E8B57',
 u'seashell': u'#FFF5EE',
 u'sienna': u'#A0522D',
 u'silver': u'#C0C0C0',
 u'skyblue': u'#87CEEB',
 u'slateblue': u'#6A5ACD',
 u'slategray': u'#708090',
 u'slategrey': u'#708090',
 u'snow': u'#FFFAFA',
 u'springgreen': u'#00FF7F',
 u'steelblue': u'#4682B4',
 u'tan': u'#D2B48C',
 u'teal': u'#008080',
 u'thistle': u'#D8BFD8',
 u'tomato': u'#FF6347',
 u'turquoise': u'#40E0D0',
 u'violet': u'#EE82EE',
 u'wheat': u'#F5DEB3',
 u'white': u'#FFFFFF',
 u'whitesmoke': u'#F5F5F5',
 u'yellow': u'#FFFF00',
 u'yellowgreen': u'#9ACD32'}

plot(xy, color-linestyle)

  • names linestyle
    • '-' or 'solid': solid line
    • '--' or 'dashed': dashed line
    • '-.' or 'dash_dot': dash-dotted line
    • ':' or 'dotted': dotted line
    • 'None': draw nothing
    • ' ': draw nothing
    • '': draw nothing

In [69]:
plt.plot([1,2,3,4], [1,4,9,16], ':')
plt.show()



In [70]:
plt.plot([1,2,3,4], [1,4,9,16], 'r--')
plt.show()


plot(xy, color-linestyle-marker)

  • '.' point marker
  • ',' pixel marker
  • 'o' circle marker
  • 'v' triangle_down marker
  • '^' triangle_up marker
  • '<' triangle_left marker
  • '>' triangle_right marker
  • '1' tri_down marker
  • '2' tri_up marker
  • '3' tri_left marker
  • '4' tri_right marker
  • 's' square marker
  • 'p' pentagon marker
  • '*' star marker
  • 'h' hexagon1 marker
  • 'H' hexagon2 marker
  • '+' plus marker
  • 'x' x marker
  • 'D' diamond marker

In [71]:
plt.plot([1,2,3,4], [1,4,9,16], 'o')
plt.show()



In [72]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.show()



In [73]:
plt.plot([1,2,3,4], [1,4,9,16], color="magenta", linestyle="--", markeredgecolor='r', markerfacecolor="orange", marker="^", markersize=20)
plt.show()



In [74]:
plt.plot([1,2,3,4], [1,4,9,16], 'r:o')
plt.show()


axis

  • axis([xmin, xmax, ymin, ymax])
  • axis('off'): turns off the axis lines and labels.:
  • axis('equal'): changes limits of x or y axis so that equal increments of x and y have the same length; a circle is circular.:
  • axis('scaled'): achieves the same result by changing the dimensions of the plot box instead of the axis data limits.:
  • axis('tight'): changes x and y axis limits such that all data is shown.

In [75]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis([0, 5, 0, 18])
plt.show()



In [76]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('off')
plt.show()



In [77]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('equal')
plt.show()



In [78]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('scaled')
plt.show()



In [79]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('tight')
plt.show()


Multiple Lines

  • plot(x1, y1, style1, x2, y2, style2, ...)

In [80]:
t = np.arange(0., 5., 0.2)
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()


Advanced Plot


In [81]:
plt.figure(figsize=(8, 6))
X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.plot(X, C, color="blue", linewidth=1.0, linestyle="-")
plt.plot(X, S, color="green", linewidth=1.0, linestyle="-")
plt.xlim(-4.0, 4.0)
plt.ylim(-1.0, 1.0)
plt.xticks(np.linspace(-4, 4, 9))
plt.yticks(np.linspace(-1, 1, 5))
plt.show()


figure

  • num: number of figure
  • figsize: figure size in in inches (width, height)

In [82]:
# %matplotlib qt
f1 = plt.figure(1, figsize=(2, 2))
plt.plot([1,2,3,4], [1,4,9,16], '-o')
f2 = plt.figure(2, figsize=(6, 6))
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.show()


plot argument names


In [83]:
plt.figure()
plt.plot(X, C, color="blue", linewidth=2, linestyle="-")
plt.plot(X, S, color="red",  linewidth=5, linestyle="--")


Out[83]:
[<matplotlib.lines.Line2D at 0x7f584585c650>]

Limit

xmin, xmax = plt.xlim() 
xlim( (xmin, xmax) )  # set the xlim to xmin, xmax
xlim( xmin, xmax )    # set the xlim to xmin, xmax

In [84]:
plt.figure()
plt.plot(X, C)
plt.xlim(X.min() * 1.1, X.max() * 1.1);
plt.ylim(C.min() * 1.1, C.max() * 1.1);



In [85]:
plt.figure()
plt.plot(X, C)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi]);
plt.yticks([-1, 0, +1]);



In [86]:
plt.figure()
plt.plot(X, C)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']);
plt.yticks([-1, 0, +1], [r'$-1$', r'$0$', r'$+1$']);


Legend


In [87]:
plt.plot(X, C, label="cosine")
plt.plot(X, S, label="sine")
plt.legend(loc='upper left')


Out[87]:
<matplotlib.legend.Legend at 0x7f5845101fd0>

Annotate


In [88]:
plt.plot(X, C, label="cosine")
t = 2 * np.pi / 3
plt.scatter(t, np.cos(t), 50, color='blue')
plt.annotate(r'$cos(\frac{2\pi}{3})=-\frac{1}{2}$', xy=(t, np.cos(t)), xycoords='data', xytext=(-90, -50), 
             textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->"))


Out[88]:
<matplotlib.text.Annotation at 0x7f584583e950>

Grid


In [89]:
plt.plot(X, C)
plt.grid(False)


xkcd style


In [90]:
with plt.xkcd():
    plt.title('XKCD style plot!!!')
    plt.plot(X, C, label="cosine")
    t = 2 * np.pi / 3
    plt.scatter(t, np.cos(t), 50, color='blue')
    plt.annotate(r'0.5 Here!', xy=(t, np.cos(t)), xycoords='data', xytext=(-90, -50), 
                 textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->"))


Advanced Settings

  • Axes
  • Axis
  • Tick

Axes

Axis

  • http://matplotlib.org/api/axis_api.html

  • cla()

  • get_label()
  • get_label_text()
  • get_major_ticks(numticks=None)
  • get_majorticklabels()
  • get_majorticklines()
  • get_majorticklocs()
  • get_minor_ticks(numticks=None)
  • get_minorticklabels()
  • get_minorticklines()
  • get_minorticklocs()
  • get_offset_text()
  • get_ticklabels(minor=False, which=None)
  • get_ticklines(minor=False)
  • get_ticklocs(minor=False)
  • grid(b=None, which='major', **kwargs)
  • set_label_text(label, fontdict=None, **kwargs)
  • set_ticklabels(ticklabels, *args, **kwargs)
  • set_ticks(ticks, minor=False)

Tick

Other plots

Scatter Plot


In [91]:
X = np.random.normal(0,1,1024)
Y = np.random.normal(0,1,1024)
plt.scatter(X,Y)


Out[91]:
<matplotlib.collections.PathCollection at 0x7f5844b4ae90>

Pie chart


In [92]:
with plt.xkcd():
    plt.figure(figsize=(5, 5))
    Z = np.random.uniform(0, 1, 20)
    plt.pie(Z, colors=mpl.cm.Set2(np.arange(20)/20.));
    plt.axis('equal');


Bar chart


In [93]:
n = 12
X = np.arange(n)
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
for x, y in zip(X, Y1):
    plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')
for x, y in zip(X, -Y2):
    plt.text(x + 0.4, y - 0.05, '%.2f' % y, ha='center', va='top')
plt.xlim(-0.25, 12)
plt.ylim(-1.25, +1.25)


Out[93]:
(-1.25, 1.25)

Histogram


In [94]:
x = np.random.randn(5000)
arrays, bins, patches = plt.hist(x, bins=50, normed=True)



In [95]:
arrays


Out[95]:
array([ 0.0014515 ,  0.002903  ,  0.        ,  0.0043545 ,  0.008709  ,
        0.005806  ,  0.011612  ,  0.017418  ,  0.02612699,  0.04209349,
        0.05080249,  0.04789949,  0.07257499,  0.10886248,  0.12482897,
        0.15095597,  0.19740396,  0.20611296,  0.26707595,  0.32223293,
        0.31207244,  0.32368443,  0.37158392,  0.32078143,  0.38029292,
        0.40206542,  0.38755042,  0.40787142,  0.40206542,  0.29900894,
        0.31497544,  0.27868794,  0.24965795,  0.24675495,  0.20901596,
        0.15095597,  0.13644097,  0.11031398,  0.07983248,  0.05370549,
        0.04644799,  0.02757849,  0.02612699,  0.0130635 ,  0.0101605 ,
        0.008709  ,  0.008709  ,  0.005806  ,  0.0014515 ,  0.002903  ])

In [96]:
bins


Out[96]:
array([-3.52617665, -3.38838813, -3.2505996 , -3.11281108, -2.97502256,
       -2.83723404, -2.69944551, -2.56165699, -2.42386847, -2.28607995,
       -2.14829142, -2.0105029 , -1.87271438, -1.73492585, -1.59713733,
       -1.45934881, -1.32156029, -1.18377176, -1.04598324, -0.90819472,
       -0.7704062 , -0.63261767, -0.49482915, -0.35704063, -0.2192521 ,
       -0.08146358,  0.05632494,  0.19411346,  0.33190199,  0.46969051,
        0.60747903,  0.74526756,  0.88305608,  1.0208446 ,  1.15863312,
        1.29642165,  1.43421017,  1.57199869,  1.70978721,  1.84757574,
        1.98536426,  2.12315278,  2.26094131,  2.39872983,  2.53651835,
        2.67430687,  2.8120954 ,  2.94988392,  3.08767244,  3.22546096,
        3.36324949])

In [97]:
patches


Out[97]:
<a list of 50 Patch objects>

Contour plot


In [98]:
def f(x, y):
    return (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 -y ** 2)

n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
XX, YY = np.meshgrid(x, y)
ZZ = f(XX, YY)
plt.contourf(XX, YY, ZZ, alpha=.75, cmap='jet')
plt.contour(XX, YY, ZZ, colors='black', linewidth=.5)


Out[98]:
<matplotlib.contour.QuadContourSet at 0x7f5844f0ef50>

Imshow


In [99]:
plt.imshow(ZZ, interpolation='nearest')
plt.grid(False)


ERROR! Session/line number was not unique in database. History logging moved to new session 14

In [100]:
plt.imshow(ZZ)
plt.grid(False)


3D surface


In [101]:
# %matplotlib qt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
XX, YY = np.meshgrid(X, Y)
RR = np.sqrt(XX**2 + YY**2)
ZZ = np.sin(RR)
ax.plot_surface(XX, YY, ZZ, rstride=1, cstride=1, cmap='hot')


Out[101]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x7f58287ea6d0>

ColorMap

  • 숫자 또는 순서를 color로 변환하는 함수 또는 표

In [108]:
mpl.cm.cmap_d


Out[108]:
{u'Accent': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772b50>,
 u'Accent_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772f10>,
 u'Blues': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772f50>,
 u'Blues_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778450>,
 u'BrBG': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772cd0>,
 u'BrBG_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772e10>,
 u'BuGn': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772110>,
 u'BuGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772d50>,
 u'BuPu': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772650>,
 u'BuPu_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772b90>,
 u'CMRmap': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2c50>,
 u'CMRmap_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772a50>,
 u'Dark2': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e24d0>,
 u'Dark2_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2750>,
 u'GnBu': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2a50>,
 u'GnBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772dd0>,
 u'Greens': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772290>,
 u'Greens_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2a10>,
 u'Greys': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772f90>,
 u'Greys_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778110>,
 u'OrRd': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772790>,
 u'OrRd_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2c10>,
 u'Oranges': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772990>,
 u'Oranges_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772410>,
 u'PRGn': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7722d0>,
 u'PRGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7780d0>,
 u'Paired': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772390>,
 u'Paired_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772150>,
 u'Pastel1': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772510>,
 u'Pastel1_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7724d0>,
 u'Pastel2': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772490>,
 u'Pastel2_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2d50>,
 u'PiYG': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772a10>,
 u'PiYG_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7783d0>,
 u'PuBu': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2ed0>,
 u'PuBuGn': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778690>,
 u'PuBuGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2650>,
 u'PuBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7727d0>,
 u'PuOr': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778650>,
 u'PuOr_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2590>,
 u'PuRd': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772ed0>,
 u'PuRd_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772590>,
 u'Purples': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778510>,
 u'Purples_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772090>,
 u'RdBu': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778250>,
 u'RdBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772c90>,
 u'RdGy': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772e90>,
 u'RdGy_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2f10>,
 u'RdPu': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2690>,
 u'RdPu_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772050>,
 u'RdYlBu': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e27d0>,
 u'RdYlBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2b50>,
 u'RdYlGn': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2d10>,
 u'RdYlGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7729d0>,
 u'Reds': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772d10>,
 u'Reds_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772e50>,
 u'Set1': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e23d0>,
 u'Set1_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778610>,
 u'Set2': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2410>,
 u'Set2_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2e50>,
 u'Set3': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2450>,
 u'Set3_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772750>,
 u'Spectral': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7fc790>,
 u'Spectral_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7725d0>,
 u'Wistia': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2bd0>,
 u'Wistia_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2350>,
 u'YlGn': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772a90>,
 u'YlGnBu': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2790>,
 u'YlGnBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772690>,
 u'YlGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778410>,
 u'YlOrBr': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7782d0>,
 u'YlOrBr_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778490>,
 u'YlOrRd': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772250>,
 u'YlOrRd_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2e10>,
 u'afmhot': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778710>,
 u'afmhot_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e25d0>,
 u'autumn': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772fd0>,
 u'autumn_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778590>,
 u'binary': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778190>,
 u'binary_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772ad0>,
 u'bone': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2c90>,
 u'bone_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772910>,
 u'brg': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772850>,
 u'brg_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2490>,
 u'bwr': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7721d0>,
 u'bwr_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778550>,
 u'cool': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2950>,
 u'cool_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2910>,
 u'coolwarm': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2310>,
 u'coolwarm_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778310>,
 u'copper': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7726d0>,
 u'copper_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e29d0>,
 u'cubehelix': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772210>,
 u'cubehelix_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778050>,
 u'flag': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772c10>,
 u'flag_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2f90>,
 u'gist_earth': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772950>,
 u'gist_earth_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772710>,
 u'gist_gray': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772bd0>,
 u'gist_gray_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772b10>,
 u'gist_heat': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772310>,
 u'gist_heat_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2b90>,
 u'gist_ncar': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2a90>,
 u'gist_ncar_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e26d0>,
 u'gist_rainbow': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2b10>,
 u'gist_rainbow_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2850>,
 u'gist_stern': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2890>,
 u'gist_stern_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2cd0>,
 u'gist_yarg': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7720d0>,
 u'gist_yarg_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2710>,
 u'gnuplot': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778210>,
 u'gnuplot2': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772890>,
 u'gnuplot2_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772610>,
 u'gnuplot_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e28d0>,
 u'gray': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2990>,
 u'gray_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772550>,
 u'hot': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2550>,
 u'hot_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2810>,
 u'hsv': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7723d0>,
 u'hsv_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772190>,
 'inferno': <matplotlib.colors.ListedColormap at 0x7f586d7fc8d0>,
 'inferno_r': <matplotlib.colors.ListedColormap at 0x7f586d7fc910>,
 u'jet': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7728d0>,
 u'jet_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2fd0>,
 'magma': <matplotlib.colors.ListedColormap at 0x7f586d7fc810>,
 'magma_r': <matplotlib.colors.ListedColormap at 0x7f586d7fc890>,
 u'nipy_spectral': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7786d0>,
 u'nipy_spectral_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778090>,
 u'ocean': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7785d0>,
 u'ocean_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772810>,
 u'pink': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778150>,
 u'pink_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2390>,
 'plasma': <matplotlib.colors.ListedColormap at 0x7f586d7fc950>,
 'plasma_r': <matplotlib.colors.ListedColormap at 0x7f586d7fc990>,
 u'prism': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778290>,
 u'prism_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772450>,
 u'rainbow': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778390>,
 u'rainbow_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d778350>,
 u'seismic': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7784d0>,
 u'seismic_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772c50>,
 u'spectral': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2f50>,
 u'spectral_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772350>,
 u'spring': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2d90>,
 u'spring_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2ad0>,
 u'summer': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e22d0>,
 u'summer_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d772d90>,
 u'terrain': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2dd0>,
 u'terrain_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2610>,
 'viridis': <matplotlib.colors.ListedColormap at 0x7f586d7fc9d0>,
 'viridis_r': <matplotlib.colors.ListedColormap at 0x7f586d7fca10>,
 u'winter': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7781d0>,
 u'winter_r': <matplotlib.colors.LinearSegmentedColormap at 0x7f586d7e2e90>}

In [109]:
cm1 = plt.get_cmap('gray')
print(cm1(0))
print(cm1(0.5))
print(cm1(1))


(0.0, 0.0, 0.0, 1.0)
(0.50196078431372548, 0.50196078431372548, 0.50196078431372548, 1.0)
(0.0039215686274509803, 0.0039215686274509803, 0.0039215686274509803, 1.0)

In [110]:
cm2 = mpl.colors.ListedColormap(['red', 'green', 'blue'])
print(cm2(np.linspace(0, 1, 10)))


[[ 1.          0.          0.          1.        ]
 [ 1.          0.          0.          1.        ]
 [ 1.          0.          0.          1.        ]
 [ 0.          0.50196078  0.          1.        ]
 [ 0.          0.50196078  0.          1.        ]
 [ 0.          0.50196078  0.          1.        ]
 [ 0.          0.          1.          1.        ]
 [ 0.          0.          1.          1.        ]
 [ 0.          0.          1.          1.        ]
 [ 0.          0.          1.          1.        ]]

In [111]:
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
plt.subplot(311);
plt.imshow(gradient, aspect='auto', cmap=plt.get_cmap('Set2'))
plt.grid(False)
plt.subplot(312);
plt.imshow(gradient, aspect='auto', cmap=cm1)
plt.grid(False)
plt.subplot(313);
plt.imshow(gradient, aspect='auto', cmap=cm2)
plt.grid(False)


Seaborn


In [112]:
d = np.random.normal(size=100)
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.distplot(d, kde=False, color="b", ax=axes[0, 0])
sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1])
sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])
sns.distplot(d, color="m", ax=axes[1, 1])


Out[112]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f5827519950>

In [113]:
df = sns.load_dataset("iris")
sns.pairplot(df, hue="species")


Out[113]:
<seaborn.axisgrid.PairGrid at 0x7f5827ee7d10>

In [114]:
tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")
sns.despine(offset=10, trim=True)


Bokeh


In [121]:
from bokeh.plotting import figure, output_notebook, show
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
output_notebook()
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
p.line(x, y, legend="Temp.", line_width=2)
show(p)


Loading BokehJS ...
Out[121]:

<Bokeh Notebook handle for In[121]>